<?php 
/*
  Plugin Name: 42 Answers - Google Maps
  Plugin URI: http://42answers.com
  Description: Op maat gemaakte google maps invoegen in pagina's en posts
  Author: J. Oldenburg
  Version: 0.2
  Author URI: http://42answers.com
*/

register_activation_hook(__FILE__,'wp42aGmaps_install');
function wp42aGmaps_install () {
  global $wpdb;
  $table_name = $wpdb->prefix . "42aGmaps";
  if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
    $sql = "CREATE TABLE " . $table_name . " (
      mapId int(10) NOT NULL AUTO_INCREMENT,
      mapType varchar(255) NOT NULL,
      mapZoom tinyint(10) NOT NULL,

      mapName varchar(255) NOT NULL,
      mapDesc text NOT NULL,

      mapLat varchar(255) NOT NULL,
      mapLon varchar(255) NOT NULL,
      mapAddress varchar(255) NOT NULL,
      mapZip varchar(255) NOT NULL,
      mapCity varchar(255) NOT NULL,

      mapIconId varchar(255) NOT NULL,
      mapIconColor text NOT NULL,

      PRIMARY KEY (mapId),
      KEY id (mapId)
    );";

    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    dbDelta($sql);
  }
}

add_action('admin_menu', 'wp42aGmaps_admin_actions');
function wp42aGmaps_admin_actions() {
//  add_options_page("42A Google Maps", "42A Gmaps", 1, "42agmaps", "wp42aGmaps_admin");
  add_submenu_page('42A-defaults/42A-defaults.php', '42A Google Maps', '42A Gmaps', 1, '42A-gmaps-admin.php', wp42aGmaps_admin);
  add_action( 'admin_init', 'wp42aSettingsGmapsRegister' );
}
function wp42aGmaps_admin() {
	include('pages/42A-gmaps-admin.php');
}

function wp42aSettingsGmapsRegister() {
  register_setting('wp42aGmapsSettingsGroup','setting42A-gmaps-apikey');
  register_setting('wp42aGmapsSettingsGroup','setting42A-gmaps-mapWidth');
  register_setting('wp42aGmapsSettingsGroup','setting42A-gmaps-mapHeight');
}




// [bartag foo="foo-value"]
function wp42aGmaps_outputShortcode($atts) {
  extract(shortcode_atts(array(
    'id' => '1',
    'scrollwheel' => '1',
  ), $atts));

//  return "foo = {$foo}";

  $query = sprintf("SELECT * FROM wp_42aGmaps WHERE mapID = $id");
  $results = mysql_query($query); 
  $count = mysql_num_rows($results);
  while ($row = mysql_fetch_array($results)) {
    $output = '

    <div id="map'. $row['mapId'] .'" style="width: '. get_option('setting42A-gmaps-mapWidth') .'px; height: '. get_option('setting42A-gmaps-mapHeight') .'px; border: 1px solid #777; overflow: hidden;"></div> 
    <script type="text/javascript">
      jQuery("#map'. $row['mapId'] .'").gMap({
        markers: [{ 
          latitude: '. $row['mapLat'] .',
          longitude: '. $row['mapLon'] .',
          html: "<div class=\"gMapsOverlayPopup\"><h4>'. $row['mapName'] .'</h4><p>'. $row['mapAddress'] .', '. $row['mapZip'] .', '. $row['mapCity'] .'<a href=\"http://maps.google.nl/maps?f=d&source=s_d&saddr='. $row['mapCity'] .','. $row['mapAddress'] .'\">Routeplanner</a></p></div>",
          popup: true,
          icon: {
            image: "/wp-content/plugins/42A-gmaps/images/gmap_pin'. $row['mapIconId'] .'_'. $row['mapIconColor'] .'.png", 
            shadow: "/wp-content/plugins/42A-gmaps/images/gmap_pin1-shadow.png", 
            iconsize: [26, 46],
            iconanchor: [12, 46],
            infowindowanchor: [12, 0]
          },
        }],
        maptype: '. $row["mapType"] .',
        zoom: '. $row["mapZoom"] .',
        scrollwheel: ' . $scrollwheel . '
      });
    </script>
    ';
    return $output;
//    return $row['mapName'] . " MAP MAP";
  }
  if ($count < 1){
    return "<div class=\"mapError\">Error displaying google maps. <sup>(is the maps id correct?!)</sup></div>"; 
  }
}
add_shortcode('wp42aGmap', 'wp42aGmaps_outputShortcode');



function wp42aGmaps_contextual_help($text) {
  $screen = $_GET['page'];
  if ($screen == '42A-gmaps-admin.php') {
    $text = "<h5>Google Maps Toevoegen of aanpassen:</h5>";
    $text .= "<p><strong>Locatie bepalen:</strong><br />Klik op de kaart om de preciese locatie te bepalen. U kunt met het scrol wieltje op de muis makkelijk en snel in en uit zoomen.<br />Het zoomniveau en de locatie van het pinnetje worden automatisch opgeslagen, en op de zelfde manier weergegeven op de website.</p>";
    $text .= "<p><strong>Algemene en adres gegeven:</strong><br />De velden in het formulier onder de kaart zijn allen verplichte velden. De informatie in deze velden word getoond in het wolk vlakje als bezoekers de google maps op de site bekijken. Vul deze belden dus secuur en correct in.</p>";
    $text .= "<p><strong>Google Maps invoegin in pagina:</strong><br />In de editor van een pagina og post kan gemakkelijk een voorgedefinieerde google map ingevoegd worden door de volgende code te plaaten: <em>[wp42aGmap id=\"1\"]</em>. Vervang het id nummer met het nummer van de door u aangemaakte map om de juiste weer tegeven in de post of pagina.</p>";
    $text .= "<p><strong>Meer informatie:</strong><br /><a href=\"http://www.42answers.com/support/wordpress/plugin/gmaps/\">42 Answers Support pagina voor deze module</a></p>";
  }
  return $text;
}
add_action('contextual_help', 'wp42aGmaps_contextual_help');





function wp42aGmaps_outputScripts() {
  wp_enqueue_style('my_meta_css', '/wp-content/plugins/42A-gmaps/lib/css/styles.admin.css');
  function addJqueryMaps() {
    echo'
    <link rel="stylesheet" type="text/css" media="all" href="/wp-content/plugins/42A-gmaps/lib/css/styles.template.css" />
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.0/jquery.min.js"></script>
    <script type="text/javascript" src="http://maps.google.com/maps?file=api&amp;v=2&amp;key='. get_option('setting42A-gmaps-apikey'). '"></script>
    <script type="text/javascript" src="/wp-content/plugins/42A-gmaps/lib/jquery/jquery.gmap-1.1.0-min.js"></script>
    ';
  }
//  wp_enqueue_script( 'jquery' );
  add_action( 'wp_head', 'addJqueryMaps');
  add_action( 'admin_init', 'wp42aSettingsRegister' );
}
add_action('init','wp42aGmaps_outputScripts');


?>
